/*
 * @(#)DefaultConfig.java, 2024/1/10 14:10
 *
 * Copyright (c) 2018-2024, 四川蜀天梦图数据科技有限公司.
 * All rights reserved.
 */
package com.zhoufan.lucene.ik.cfg;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * DefaultConfig
 *
 * @author fantazy
 * @version 1.0, 2024年01月10日
 */
public class DefaultConfig implements Configuration
{
    private static final String PATH_DIC_MAIN = "com/zhoufan/lucene/ik/dic/main2012.dic";

    private static final String PATH_DIC_QUANTIFIER = "com/zhoufan/lucene/ik/dic/quantifier.dic";

    private static final String FILE_NAME = "IKAnalyzer.cfg.xml";

    private static final String EXT_DICT = "ext_dict";

    private static final String EXT_STOP = "ext_stopwords";

    private Properties props;

    private boolean useSmart;

    public static Configuration getInstance()
    {
        return new DefaultConfig();
    }

    private DefaultConfig()
    {
        props = new Properties();
        final InputStream input = this.getClass().getClassLoader().getResourceAsStream(FILE_NAME);
        if (null != input)
        {
            try
            {
                props.loadFromXML(input);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

    }

    @Override
    public boolean useSmart()
    {
        return useSmart;
    }

    @Override
    public void setUseSmart(boolean useSmart)
    {
        this.useSmart = useSmart;
    }

    @Override
    public String getMainDictionary()
    {
        return PATH_DIC_MAIN;
    }

    @Override
    public String getQuantifierDicionary()
    {
        return PATH_DIC_QUANTIFIER;
    }

    @Override
    public List<String> getExtDictionarys()
    {
        List<String> extDictFiles = new ArrayList<String>(2);
        String extDictCfg = props.getProperty(EXT_DICT);
        if (extDictCfg != null)
        {
            // 使用;分割多个扩展字典配置
            String[] filePaths = extDictCfg.split(";");
            if (filePaths != null)
            {
                for (String filePath : filePaths)
                {
                    if (filePath != null && !"".equals(filePath.trim()))
                    {
                        extDictFiles.add(filePath.trim());
                    }
                }
            }
        }
        return extDictFiles;
    }

    @Override
    public List<String> getExtStopWordDictionarys()
    {
        List<String> extStopWordDictFiles = new ArrayList<String>(2);
        String extStopWordDictCfg = props.getProperty(EXT_STOP);
        if (extStopWordDictCfg != null) {
            // 使用;分割多个扩展字典配置
            String[] filePaths = extStopWordDictCfg.split(";");
            if (filePaths != null) {
                for (String filePath : filePaths) {
                    if (filePath != null && !"".equals(filePath.trim())) {
                        extStopWordDictFiles.add(filePath.trim());
                    }
                }
            }
        }
        return extStopWordDictFiles;
    }
}
